package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.db.domain.web.MpClientAddress;
import com.ruoyi.db.mapper.web.MpClientAddressMapper;
import com.ruoyi.mp.service.IMpClientAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 【用户收货地址】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-26
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientAddressServiceImpl implements IMpClientAddressService
{
    @Autowired
    private MpClientAddressMapper mpClientAddressMapper;

    /**
     * 查询【用户收货地址】
     * 
     * @param addressId 【用户收货地址】主键
     * @return 【用户收货地址】
     */
    @Override
    public MpClientAddress selectMpClientAddressByAddressId(Long addressId)
    {
        return mpClientAddressMapper.selectMpClientAddressByAddressId(addressId);
    }

    /**
     * 查询【用户收货地址】列表
     * 
     * @param mpClientAddress 【用户收货地址】
     * @return 【用户收货地址】
     */
    @Override
    public List<MpClientAddress> selectMpClientAddressList(MpClientAddress mpClientAddress)
    {
        return mpClientAddressMapper.selectMpClientAddressList(mpClientAddress);
    }

    /**
     * 新增【用户收货地址】
     * 
     * @param mpClientAddress 【用户收货地址】
     * @return 结果
     */
    @Override
    public int insertMpClientAddress(MpClientAddress mpClientAddress)
    {
        mpClientAddress.setCreateTime(DateUtils.getNowDate());
        return mpClientAddressMapper.insertMpClientAddress(mpClientAddress);
    }

    /**
     * 修改【用户收货地址】
     * 
     * @param mpClientAddress 【用户收货地址】
     * @return 结果
     */
    @Override
    public int updateMpClientAddress(MpClientAddress mpClientAddress)
    {
        if(mpClientAddress.getIsDefault()!=null&&mpClientAddress.getIsDefault()==1){
            MpClientAddress address = new MpClientAddress();
            address.setIsDefault(1);
            address.setClientId(mpClientAddress.getClientId());
            List<MpClientAddress> updList = mpClientAddressMapper.selectMpClientAddressList(address);
            for (MpClientAddress address1: updList) {
                address1.setIsDefault(0);{
                    mpClientAddressMapper.updateMpClientAddress(address1);
                }
            }
        }
        return mpClientAddressMapper.updateMpClientAddress(mpClientAddress);
    }


}
